package http

import (
	"errors"
	"gitee.com/saxon134/go-utils/saData"
	"gitee.com/saxon134/go-utils/saLog"
	"gitee.com/saxon134/go-utils/saTask"
	"gitee.com/saxon134/workflow/api"
	"github.com/gin-gonic/gin"
)

// Index handler，避免每个项目都搞一个
func Index(c *api.Context) (res *api.Response, err error) {
	return &api.Response{Result: "hii"}, nil
}

func SetLogLevel(c *api.Context) (res *api.Response, err error) {
	gc, _ := c.Context.(*gin.Context)
	if gc == nil {
		err = errors.New("仅支持HTTP请求")
		return
	}

	level, _ := saData.ToInt(gc.PostForm("level"))
	if level >= 1 && level <= 3 {
		saLog.SetLogLevel(saLog.LogLevel(level))
	}

	return
}

func RunTask(c *api.Context) (res *api.Response, err error) {
	gc, _ := c.Context.(*gin.Context)
	if gc == nil {
		err = errors.New("仅支持HTTP请求")
		return
	}

	name := gc.PostForm("name")
	if name == "" {
		ResErr(c, "任务名称不能空")
		return
	}

	err = saTask.Fire(name)
	if err != nil {
		ResErr(c, err)
		return
	}

	res = &api.Response{Result: "开始执行任务"}
	return
}
